package com.xiaobawang.hundouluo.service;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import com.xiaobawang.hundouluo.bean.Environment;
import com.xiaobawang.hundouluo.bean.NetCallback;
import com.xiaobawang.hundouluo.utils.UrlUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;


public class DataService extends IntentService {

    public DataService() {
        super("DataService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            String type = intent.getStringExtra("type");
            assert type != null;
            if (type.equalsIgnoreCase("interval")) {
                String sensorName = intent.getStringExtra("sensorName");
                String interval = intent.getStringExtra("interval");
                String rowNum = intent.getStringExtra("rowNum");
                Log.e("DataService-inter", sensorName + "," + interval + "," + rowNum);
                interval(sensorName, interval, rowNum);
            } else if (type.equalsIgnoreCase("realtime")) {
                realtime();
            }
        }
    }

    String dataUnit = "";
    double[] datas = null;

    private void interval(final String sensorName, String interval, String rowNum) {
        RequestParams params = new RequestParams(UrlUtil.DATA_INTERVAL);
        params.addBodyParameter(UrlUtil.PARAM_SENSORNAME, sensorName);
        params.addBodyParameter(UrlUtil.PARAM_INTERVAL, interval);
        params.addBodyParameter(UrlUtil.PARAM_ROWNUM, rowNum);
        x.http().post(params, new NetCallback("DataService-Interval") {
            @Override
            public void onSuccess(String result) {

                Log.e("DataService-interval", result);

                try {
                    JSONObject rootObj = new JSONObject(result);
                    String status = rootObj.getString("status");
                    if (status.equalsIgnoreCase("success")) {
                        JSONArray dataArr = rootObj.getJSONArray("results");
                        dataUnit = dataArr.getJSONObject(0).getString("dataUnit");
                        datas = new double[dataArr.length()];
                        for (int i = 0; i<dataArr.length(); i++) {
                            JSONObject item = dataArr.getJSONObject(i);
                            datas[i] = item.getDouble("data");
                        }
                    }

                    Intent bcIntent = new Intent("com.xiaobawang.hundouluo.receiver.data.interval");
                    bcIntent.putExtra("status", status);
                    bcIntent.putExtra("dataUnit", dataUnit);
                    bcIntent.putExtra("datas", datas);
                    sendBroadcast(bcIntent);

                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });
    }

    private void realtime() {
        RequestParams params = new RequestParams(UrlUtil.DATA_NOW);
        x.http().post(params, new NetCallback("DataService-Realtime") {
            @Override
            public void onSuccess(String result) {
                Log.e("DataService-realtime", result);
                try {
                    JSONObject rootObj = new JSONObject(result);
                    String status = rootObj.getString("status");
                    ArrayList<Environment> list = new ArrayList<>();
                    if (status.equalsIgnoreCase("success")) {
                        JSONArray dataArr = rootObj.getJSONArray("results");
                        for (int i = 0; i < dataArr.length(); i++) {
                            JSONObject item = dataArr.getJSONObject(i);
                            Environment environment = new Environment(null,
                                    item.getDouble("data"),
                                    item.getString("dataUnit"),
                                    item.getString("sensorName"));
                            list.add(environment);
                        }
                    }

                    Intent bcIntent = new Intent("com.xiaobawang.hundouluo.receiver.data.realtime");
                    bcIntent.putExtra("status", status);
                    bcIntent.putExtra("datas", list);
                    sendBroadcast(bcIntent);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
